<?php
class DealWebTestCase extends CakeWebTestCase
{
    function title($tag, $message) {
        echo '<'.$tag.'>'.$message.'</'.$tag.'>';
    }
    function homepage($loggedIn = false, $admin = false) {
        $this->title('h3', 'Enter homepage');
        $this->assertTrue($this->get('http://localhost/dealcruncher'));

        // Test if all the css files are loaded
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/home.css"/');
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/menu.css"/');
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/search.css"/');
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/tango_skin.css"/');
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/jquery.jgrowl.css"/');
        $this->assertPattern('/\<link rel="stylesheet" type="text\/css" href="\/dealcruncher\/css\/jquery-ui-1.9.0.custom.min.css"/');

        // Test if all the javascript files are loaded properly
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/jquery-1.8.2.min.js"/');
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/jquery.jgrowl.js"/');
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/home.js"/');
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/login.js"/');
        $this->assertPattern('/\<script type="text\/javascript" src="\/dealcruncher\/js\/register.js"/');

        // Check the menu
        $this->assertLink('Home');
        $this->assertLink('Advance Search');
        $this->assertLink('Recommendations');
        $this->assertLink('News');
        if ($admin) {
            $this->assertLink('Add product');
        } else {
            $this->assertNoLink('Add product');
        }
        if (!$loggedIn) {
            $this->assertLink('Login');
            $this->assertLink('SIGN UP');
            $this->assertNoLink('Cart');
            $this->assertNoLink('Logout');
        }
        else {
            $this->assertLink('Cart');
            $this->assertLink('Logout');

            $this->assertNoLink('Login');
            $this->assertNoLink('SIGN UP');
        }

        // Check page content
        $this->assertText('BEST RATED');
        $this->assertText('MOST POPULAR');
    }
    function login($email, $password) {
        $this->title('h3', 'Login');
        $this->clickLink('Login');
        $this->assertText('Email');
        $this->assertText('Password');

        $this->setFieldById('UserUsername', $email);
        $this->setFieldById('UserPassword', $password);

        $this->assertClickable('Log In');
        $this->clickSubmit('Log In');
        $this->assertNoText('Invalid email or password. Please try again');
    }

    function _testSet1() {
        $this->title('h2', 'Test Case 1 - Add Deal, Cancel Request');
        $this->homepage();
        $this->title('h3', 'Add deal with no permission');
        $this->assertTrue($this->get('http://localhost/dealcruncher/deals/add'));
        $this->assertEqual($this->getUrl(), 'http://localhost/dealcruncher/');

        $this->homepage();
        $this->login('invincible_kid23@yahoo.com', '123456');
        $this->title('h3', 'Add deal with permission');
        $this->assertTrue($this->get('http://localhost/dealcruncher/deals/add'));
        $this->assertEqual($this->getUrl(), 'http://localhost/dealcruncher/deals/add');
    }

    function _testSet2() {
        $this->title('h2', 'Test Case 2 - Add Deal, Cancel Request');
        $this->homepage();
        $this->title('h3', 'Add deal with no permission');
        $this->assertTrue($this->get('http://localhost/dealcruncher/deals/add'));
        $this->assertEqual($this->getUrl(), 'http://localhost/dealcruncher/');

        $this->homepage();
        $this->login('invincible_kid23@yahoo.com', '123456');
        $this->title('h3', 'Add deal with permission');
        $this->assertTrue($this->get('http://localhost/dealcruncher/deals/add'));
        $this->assertEqual($this->getUrl(), 'http://localhost/dealcruncher/deals/add');

        $this->assertField('data[Deal][product_id]', 1);
        $this->assertField('data[Deal][price]', '');
        $this->setField('data[Deal][price]', '100');
        $this->assertField('data[Deal][price]', '100');
        $this->assertField('data[Deal][description]', '');
        $this->setField('data[Deal][description]', 'Some description here');
        $this->assertField('data[Deal][description]', 'Some description here');

        $this->assertClickable('Save and publish');
        $this->clickSubmitById('save_publish');
    }

    function testSet3() {
        $this->title('h2', 'Test Case 3 - Add Deal to Cart');
        $this->homepage();
        $this->login('ngthanhtrung23@gmail.com', '123456');
        $this->title('h3', 'Add deal to cart');
        $this->assertTrue($this->get('http://localhost/dealcruncher/deals/product_deals/1'));
        $this->clickSubmitById('130_add');
        $this->clickSubmitById('13_add');

        $this->title('h3', 'Check cart');

        $this->assertTrue($this->get('http://localhost/dealcruncher/users/cart'));
    }

    function _testSet4() {
        $this->title('h2', 'Test Case 4 - View list of deal in cart and pay the products');
        $this->homepage();
        $this->login('ngthanhtrung23@gmail.com', '123456');

        $this->title('h3', 'View list of deal in cart');
        $this->assertTrue($this->get('http://localhost/dealcruncher/users/cart'));
        $this->title('h3', 'Checkout');
        $this->assertClickable('Proceed to payment');
    }
}
?>
